package com.budwk.app.sso.controllers;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.sso.SaSsoProcessor;
import cn.dev33.satoken.sso.SaSsoUtil;
import cn.dev33.satoken.stp.StpUtil;

import cn.dev33.satoken.util.SaResult;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUnitService;
import com.budwk.app.uc.services.AuthService;
import com.budwk.starter.common.result.Result;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/sso")
public class SsoClientController {

    @Inject
    AuthService authService;
    @Inject
    private SysUnitService sysUnitService;

    //当前是否登录
    @At("/isLogin")
    @Ok("json")
    public Object isLogin() {
        System.out.println("登录状态："+StpUtil.isLogin());
        return Result.data(StpUtil.isLogin());
    }

    // 返回SSO认证中心登录地址
    @At("/getSsoAuthUrl")
    @Ok("json")
    public Result getSsoAuthUrl(@Param("clientLoginUrl") String clientLoginUrl) {
        String serverAuthUrl = SaSsoUtil.buildServerAuthUrl(clientLoginUrl, "");
        return Result.data(serverAuthUrl);
    }

    // 根据ticket进行登录
    @At("/doLoginByTicket")
    @Ok("json")
    public Result doLoginByTicket(@Param("ticket")String ticket) {
        Object loginId = SaSsoProcessor.instance.checkTicket(ticket, "/sso/doLoginByTicket");
        if(loginId != null) {
            StpUtil.login(loginId);
            Sys_user user = authService.getUserById(loginId.toString());
            SaSession session = StpUtil.getSession(true);
            session.set("loginname", Strings.sNull(user.getLoginname()));
            session.set("username", Strings.sNull(user.getUsername()));
            session.set("unitId", Strings.sNull(user.getUnitId()));
            session.set("unitPath", Strings.sNull(user.getUnitPath()));
            return Result.data(StpUtil.getTokenValue());
        }
        return Result.error("无效ticket：" + ticket);
    }

    //客户端sso-client
    @At("/login")
    @Ok("re")
    public Object login(){
        System.out.println("客户端登录");
        Object object = SaSsoProcessor.instance.ssoLogin();
        if (object == null) {
            object = "";
        }
        return object;
    }

    //单点注销地址
    @At("/logout")
    @Ok("raw")
    public Object logout(){
        System.out.println("客户端单点注销");
        System.out.println("客户端登录状态：" + StpUtil.isLogin());
        Object object = SaSsoProcessor.instance.ssoLogout();
        if (object == null) {
            object = "";
        }
        if (object instanceof SaResult) {
            SaResult saResult = (SaResult) object;
            if (saResult.getCode() == SaResult.CODE_SUCCESS) {
                saResult.setCode(0);
                object = saResult;
            }
        }
        return object;
    }

    //单点注销回调
    @At("/logoutCall")
    @Ok("json")
    public Object logoutCall(){
        System.out.println("客户端单点注销回调");
        return SaSsoProcessor.instance.ssoLogoutCall();
    }

}
